<script setup lang="ts">
import axios from 'axios'
import { ref, inject, reactive, onBeforeMount } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const BaseUrl = inject('BaseUrl');
// 登陆加载动画
let nowLoading = ref(false)
// 登陆是否成功
let loadingState = ref(false)
// 登陆结果
let loadRes = ref("")
const userInfo = reactive({
    userName: '',
    password: '',
})
onBeforeMount(() => {
    router.push({ path: '/index/userInfo' })
    // axios.post(BaseUrl + '/vx/api/isLogin')
    //     .then(res => {
    //         // 开发模式 - 上线改回 res.data.code == 200
    //         if (true) {
    //             router.push({ path: '/index/userInfo' })
    //         } else {
    //             console.log(res.data.msg);
    //         }
    //     }).catch(res => {
    //         res
    //         router.push({ path: '/index/userInfo' })
    //     })
})
const login = (e: any) => {
    e.preventDefault()
    if (userInfo.userName == '' || userInfo.password == '') {
        loadRes.value = '请输入用户名和密码'
        loadingState.value = true;
        setInterval(() => {
            loadingState.value = false;
        }, 3000)
        return
    }
    // 动画出现
    nowLoading.value = true

    axios.post(BaseUrl + "/vx/api/login", {
        username: userInfo.userName,
        password: userInfo.password,
    }, { headers: { 'Content-type': 'application/json', charset: 'UTF-8' } }).then(res => {
        const data = res.data;
        if (data.code == "200") {
            nowLoading.value = false;
            // 成功路由
            router.push({ path: '/index' })
        } else {
            // 失败
            loadRes.value = data.msg;
            nowLoading.value = false;
            loadingState.value = true;
            setInterval(() => {
                loadingState.value = false;
            }, 3000)
        }
    })
}
</script>
<template>
    <form id="login-box">
        <label for="username">用户名</label>
        <input type="text" id="username" v-model="userInfo.userName">
        <label for="password">密码</label>
        <input type="password" id="password" v-model="userInfo.password">
        <div v-if="nowLoading" class="loading"></div>
        <div style="display: flex;justify-content: space-between; width: 50%;">
            <span v-if="loadingState">{{ loadRes }}</span>
            <button v-else id="loginBtn" @click="login">登陆</button>
            <button id="registerBtn">注册</button>
        </div>
    </form>
</template>
<style scoped lang="css">
#loginBtn,
#registerBtn {
    margin-top: 0.6em;
    border-radius: 8px;
    border: 1px solid transparent;
    padding: 0.4em 1.0em;
    font-size: 1em;
    font-weight: 500;
    font-family: inherit;
    background-color: #1a1a1a;
    cursor: pointer;
    transition: border-color 0.25s;

}



#loginBtn:hover,
#registerBtn:hover {
    border-color: #646cff;
}

#loginBtn:focus,
#loginBtn:focus-visible,
#registerBtn:focus,
#registerBtn:focus-visible {
    outline: 4px auto -webkit-focus-ring-color;
}

#login-box {
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    width: 400px;
    height: 200px;
    background-color: rgba(9, 9, 9, 0.5);
    padding-left: auto;
    margin: 0 auto;
    border-radius: 5px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

#login-box input {
    height: 10%;
    width: 55%;
    border-radius: 8px;
    margin-bottom: 0.4em;
}

#login-box label {
    margin-bottom: 0.4em;
}

.loading {
    position: relative;
    width: 30px;
    height: 30px;
    border: 2px solid #000;
    border-top-color: rgba(0, 0, 0, 0.2);
    border-right-color: rgba(0, 0, 0, 0.2);
    border-bottom-color: rgba(0, 0, 0, 0.2);
    border-radius: 100%;
    animation: circle infinite 0.75s linear;
}

@keyframes circle {
    0% {
        transform: rotate(0);
    }

    100% {
        transform: rotate(360deg);
    }
}
</style>